#include "net_include.h"
#include "common.h"

void printPacket(char* packet) {
    Header *recvHdr= (Header*) packet;
    printf("[ Type= %d | Pid=%d | PIdx=%d | Ctr=%d | ldCtr=%d ] \n", recvHdr->packetType, recvHdr->pid, recvHdr->idx, recvHdr->counter, recvHdr->lastDeliveredCtr);
}

int main()
{
    struct sockaddr_in name;
    struct sockaddr_in send_addr;

    int                mcast_addr;

    struct ip_mreq     mreq;
    unsigned char      ttl_val;

    int                ss;
    fd_set             mask;
    fd_set             dummy_mask,temp_mask;
    int                bytes;
    int                num;
    char               mess_buf[MAX_MESS_LEN];
    Header  *hdr=(Header*)mess_buf;
    
    mcast_addr = MCAST_IP; /* (225.0.1.1) */

    name.sin_family = AF_INET;
    name.sin_addr.s_addr = INADDR_ANY;
    name.sin_port = htons(PORT);

    mreq.imr_multiaddr.s_addr = htonl( mcast_addr );

    /* the interface could be changed to a specific interface if needed */
    mreq.imr_interface.s_addr = htonl( INADDR_ANY );

    
    ss = socket(AF_INET, SOCK_DGRAM, 0); /* Socket for sending */
    if (ss<0) {
        perror("Bcast: socket");
        exit(1);
    }

    ttl_val = 1;
    if (setsockopt(ss, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl_val,
        sizeof(ttl_val)) < 0)
    {
        printf("Mcast: problem in setsockopt of multicast ttl %d - ignore in WinNT or Win95\n", ttl_val );
    }

    send_addr.sin_family = AF_INET;
    send_addr.sin_addr.s_addr = htonl(mcast_addr);  /* mcast address */
    send_addr.sin_port = htons(PORT);

    hdr->packetType = TYPE_START_MCAST;
    sendto( ss, mess_buf, strlen(mess_buf), 0,
                    (struct sockaddr *)&send_addr, sizeof(send_addr) );
    printf("--> START MULTICAST: ");
    printPacket(mess_buf);

    return 0;
}

